#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''decompress ROS Node'''
# license removed for brevity
import rospy
from std_msgs.msg import String

import face_recognition
import cv2
import numpy


def face():
    '''decompress Publisher'''
    pub_tts = rospy.Publisher('tts', String, queue_size=10)
    rospy.init_node('face_detection', anonymous=True)
    rate = rospy.Rate(5)

    video_capture = cv2.VideoCapture(0)
    process_this_frame = True
    detec_status = False
    array = [0,0,0,0,0,0,0,0,0,0]
    pos = 0
    while not rospy.is_shutdown():
        ret, frame = video_capture.read()
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
        if process_this_frame:
            face_locations = face_recognition.face_locations(small_frame)
            if len(face_locations) > 0:
                array[pos] = 1               
            else:
                array[pos] = 0
            pos = pos + 1
            if pos >= len(array):
                pos = 0
            if numpy.mean(array) > 0.5:
                if detec_status is False:
                    pub_tts.publish("你好")
                    detec_status = True
            else:
                detec_status = False

        process_this_frame = not process_this_frame
        rate.sleep()

if __name__ == '__main__':
    try:
        face()
    except rospy.ROSInterruptException:
        pass
